/*
 * @lc app=leetcode.cn id=299 lang=cpp
 *
 * [299] 猜数字游戏
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
    string getHint(string secret, string guess) {
        int bulls = 0;
        map<char, int> ms, mg;
        for(int i = 0; i < secret.length(); i++)
        {
            if(secret[i] == guess[i])
            {
                bulls++;
            }
            else
            {
                ms[secret[i]]++;
                mg[guess[i]]++;
            }
        }
        int cows = 0;
        for(auto&p : ms)
        {
            auto it = mg.find(p.first);
            if(it != mg.end())
            {
                cows += std::min(p.second, it->second);
            }
        }
        string ret = to_string(bulls);
        ret += 'A';
        ret += to_string(cows);
        ret += 'B';
        return ret;
    }
};
// @lc code=end

